package cn.xiangxiang.service.impl;

import cn.xiangxiang.mapper.goods.BrandMapper;
import cn.xiangxiang.mapper.goods.CategoryMapper;
import cn.xiangxiang.mapper.goods.SkuMapper;
import cn.xiangxiang.mapper.goods.SpuMapper;
import cn.xiangxiang.pojo.goods.*;
import cn.xiangxiang.service.goods.SkuService;
import cn.xiangxiang.service.goods.SpuService;
import cn.xiangxiang.service.utils.IdWorker;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = SpuService.class)
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {

    @Autowired
    private SkuMapper skuMapper;

    @Autowired
    private SkuService skuService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Page<Spu> findConditionPage(Integer startpage, Integer pagesize, Spu spu) {
        QueryWrapper<Spu> wrapper = getCondition(spu);
        Page<Spu> page = new Page<>(startpage, pagesize);
        Page<Spu> spuPage = page(page, wrapper);
        return spuPage;
    }

    @Override
    public Goods findsgoodsbyid(String spuid) {
        Spu spu = getById(spuid);
        HashMap<String, Object> map = new HashMap<>();
        map.put("spu_id",spuid);
        List<Sku> skuList = skuMapper.selectByMap(map);
        Goods goods = new Goods();
        goods.setSpu(spu);
        goods.setSkuList(skuList);
        return goods;
    }

    @Transactional
    public void updateSpuSkuList(Goods goods) {
        Spu spu = goods.getSpu();
        if(spu.getId()==null){
            spu.setId(idWorker.nextId()+"");
        }
        if(spu.getNewSpecItems()!=null){
            spu.setSpecItems(spu.getNewSpecItems());

            saveOrUpdate(spu);

            // 删除sku列表
            HashMap<String, Object> map = new HashMap<>();
            map.put("spu_id",spu.getId());
            skuMapper.deleteByMap(map);


            ArrayList<Sku> skus = new ArrayList<>();

            List<Map<String, Object>> specList = goods.getSpecList();
            for(int i =0; i<specList.size();i++){

                Sku sku = new Sku();
                sku.setId(idWorker.nextId()+"");
                sku.setSpuId(spu.getId());

                Map<String, Object> specMap = specList.get(i);
                
                Collection<Object> values = specMap.values();

                String name = spu.getName();

                for (Object val : values) {
                    name = name +" " +val;
                }

                sku.setName(name);

                sku.setCreateTime(new Date());
                sku.setUpdateTime(new Date());
                Brand brand = brandMapper.selectById(spu.getBrandId());
                sku.setBrandName(brand.getName());

                sku.setCategoryId(spu.getCategory3Id());

                Category category = categoryMapper.selectById(spu.getCategory3Id());
                sku.setCategoryName(category.getName());

                sku.setSpec(JSON.toJSONString(specMap));

                sku.setStatus("2");

                skus.add(sku);

            }

            skuService.saveBatch(skus);

        }else{
            saveOrUpdate(spu);
        }

        rabbitTemplate.convertAndSend("xiangxiang.pages",spu.getId());

    }

    @Transactional
    public void delByIdAndSkuList(String id) {
        Spu spu = getById(id);
        spu.setStatus("1");
        spu.setIsDelete("1");
        spu.setIsMarketable("1");
        updateById(spu);

        HashMap<String, Object> map = new HashMap<>();
        map.put("spu_id",id);
        List<Sku> skuList = skuService.listByMap(map);
        skuList.forEach((sku)->{
            sku.setStatus("3");
            sku.setUpdateTime(new Date());
        });

        skuService.updateBatchById(skuList);

    }

    private QueryWrapper<Spu> getCondition(Spu spu){
        QueryWrapper<Spu> wrapper = new QueryWrapper<>();
        if (spu!=null) {
            if(spu.getName()!=null && !"".equals(spu.getName())){
                wrapper.like("name",spu.getName());
            }
            if(spu.getId()!=null && !"".equals(spu.getId())){
                wrapper.eq("id",spu.getId());
            }
            if(spu.getStatus()!=null && !"".equals(spu.getStatus())){
                wrapper.eq("status",spu.getStatus());
            }
            if(spu.getIsDelete()!=null && !"".equals(spu.getIsDelete())){
                wrapper.eq("is_delete",spu.getIsDelete());
            }
        }
        return wrapper;
    }
}
